import json
import logging
import requests, pymysql
from bs4 import BeautifulSoup

import base64
import hashlib
import json
import re
import traceback

from Crypto.Cipher import AES

import app


def assert_utils(self, response, status_code, status, desc):
    self.assertEqual(response.status_code, status_code)
    self.assertEqual(response.json().get("status"), status)
    self.assertEqual(response.json().get("description"), desc)

def third_requests(self, form_data):
    # 定义Beautiful对象
    soup = BeautifulSoup(form_data, "html.parser")
    # 获取第三方接口的URL地址
    third_url = soup.form['action']
    logging.info("get response = {}".format(third_url))
    # 定义字典获取参数值
    data = {}
    for i in soup.find_all('input'):
        data.setdefault(i["name"], i["value"])
    # 发送请求
    response = requests.post(third_url, data=data)
    return response


class DB_mysql:
    @classmethod
    def get_conn(cls, db_name):
        conn = pymysql.connect(host=app.DB_URL, port=3306, user=app.DB_username, password=app.DB_password, db=db_name, autocommit=True)
        return conn

    @classmethod
    def close(cls, cursor, conn):
        if cursor:
            cursor.close()
        if conn:
            conn.close()

    @classmethod
    def delete(cls, db_name, sql):
        try:
            conn = cls.get_conn(db_name)
            cursor = conn.cursor()
            cursor.execute(sql)
        except Exception as e:
            conn.rollback()
        finally:
            cls.close(cursor, conn)

def read_Imgcode_data(filename):
    file = app.base_dir + "/data/" + filename
    test_data = []
    with open(file, encoding='utf-8') as f:
        data = json.load(f)
        list_data = data.get("get_imgcode")
        for test_list_data in list_data:
            test_data.append((test_list_data.get("type"), test_list_data.get("status_code")))
    print("json data = {}".format(test_data))
    return test_data

# 加解密工具类
class EncryptUtil:
    # 发送请求时，加密密码
    SEND_AES_KEY = ";3jm$>/p-ED^cVz_j~.KV&V)k9jn,UAH"
    # 发送请求时，签名密钥
    SEND_SIGN_KEY = "DY34fdgsWET@#$%wg#@4fgd345sg"
    # 接收数据时，解密密钥
    RECEIVE_AES_KEY = "54Ms5bkE6UEdyrRviJ0![OR]g+i79x]k"

    @staticmethod
    def padding_pkcs5(value):
        BS = AES.block_size
        return str.encode(value + (BS - len(value) % BS) * chr(BS - len(value) % BS))

    # 替换空字符
    @staticmethod
    def replace_blank(str_data):
        str_data = re.compile("\t|\r|\n").sub("", str_data)
        print("replace_blank str_data=", str_data)
        return str_data

    @staticmethod
    def aes_encrypt(key, data):
        """
        AES加密
        :param key: 密钥
        :param data: 待加密数据
        :return: 加密后数据
        """
        data = base64.encodebytes(data.encode()).decode()
        # 替换特殊字符
        data = EncryptUtil.replace_blank(data)
        print("data=", data)

        # 初始化加密器
        aes = AES.new(key.encode(), AES.MODE_ECB)

        # 加密
        padding_value = EncryptUtil.padding_pkcs5(data)
        encrypt_aes = aes.encrypt(padding_value)

        # 用base64转成字符串形式
        encrypted_text = base64.encodebytes(encrypt_aes).decode()
        return encrypted_text

    @staticmethod
    def aes_decrypt(key, data):
        """
        AES解密
        :param key: 密钥
        :param data: 待解密数据
        :return: 解密后数据
        """
        # 初始化加密器
        aes = AES.new(key.encode(), AES.MODE_ECB)
        # 优先逆向解密base64成bytes
        base64_decrypted = base64.decodebytes(data.encode())

        # 执行解密
        decrypted_bytes = base64.decodebytes(aes.decrypt(base64_decrypted))
        # 转换为字符串
        decrypted_text = str(decrypted_bytes, encoding="utf-8")

        # 把Unicode转成中文
        result = decrypted_text.encode().decode("unicode_escape")
        return result

    @staticmethod
    def md5value(data):
        print("md5value data=", data)
        md5 = hashlib.md5()
        md5.update(data.encode())
        return md5.hexdigest()

    # 加密调用
    @staticmethod
    def get_diyou(data):
        # 把字典转换为JSON字符串
        if isinstance(data, dict):
            data = json.dumps(data)
        aes_encrypt_data = EncryptUtil.aes_encrypt(EncryptUtil.SEND_AES_KEY, data)
        return EncryptUtil.replace_blank(aes_encrypt_data)

    # 签名调用
    @staticmethod
    def get_xmdy(data):
        return EncryptUtil.md5value(
            EncryptUtil.SEND_SIGN_KEY + EncryptUtil.replace_blank(data) + EncryptUtil.SEND_SIGN_KEY)

    # 解密调用
    @staticmethod
    def decrypt_data(data):
        return EncryptUtil.aes_decrypt(EncryptUtil.RECEIVE_AES_KEY, data)


if __name__ == '__main__':
    # 加密
    send_data = {}
    content = json.dumps(send_data)
    diyou = EncryptUtil.get_diyou(content)
    print("diyou=", diyou)
    xmdy = EncryptUtil.get_xmdy(diyou)
    print("xmdy=", xmdy)

def encryted_Request(url,req_data):
    # 对数据进行加密
    diyou = EncryptUtil.get_diyou(req_data)
    xmdy = EncryptUtil.get_xmdy(diyou)
    # 发送请求
    req_param = {"diyou": diyou, "xmdy": xmdy}
    response = requests.post(url, data=req_param)
    # 接收响应并解密
    diyou_data = response.json().get("diyou")
    data = EncryptUtil.decrypt_data(diyou_data)
    result = json.loads(data)
    # 返回解密后结果
    return result